//百度地图AK设置模块
SUCCESS_TEXT = "设置成功。请断开连接并停止推流，重新连接方可生效。";
FAILURE_TEXT = "设置失败。";
EMPTY_TEXT = "不能为空。";

function set_ak_tip(success, content) {
    const target = document.getElementById("setak_tip");
    if (success === true) {
        target.classList.remove("text-danger");
        target.classList.add("text-success");
    } else {
        target.classList.remove("text-success");
        target.classList.add("text-danger");
    }
    target.innerText = content;
    target.style.display = "block";
}

function submit_general() {
    let new_server = document.getElementById("server").value;
    let new_pusher = document.getElementById("pusher").value;
    let new_id = document.getElementById("id").value;
    let new_key = document.getElementById("key").value;
    if (new_server === '' || new_pusher === '' || new_id === '' || new_key === '') {
        set_ak_tip(false, EMPTY_TEXT);
        return;
    }
    fetch('/api/general', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            server: new_server,
            pusher: new_pusher,
            id: new_id,
            key: new_key
        })
    }).then(response => response.json())
    .then(data => {
        if (data.success === true) {
            set_ak_tip(true, SUCCESS_TEXT);
        } else {
            set_ak_tip(false, FAILURE_TEXT);
        }
    }).catch((error) => {
        set_ak_tip(false, "Error:"+FAILURE_TEXT);
    });
}